package newWork.controller;

import newWork.constant.ResultConstant;
import newWork.dto.staffSet;
import newWork.entity.*;
import newWork.service.IProjectService;
import newWork.service.IStaffProjectService;
import newWork.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/project")
public class ProjectController {


    @Autowired
    private IProjectService service;

    @Autowired
    private IStaffService staffService;

    @Autowired
    private IStaffProjectService staffProjectService;

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public CommonResult select(@RequestBody ProjectEntity entity) {
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG, service.select(entity));
    }

    /**
     * 查询列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public LayUITable<ProjectEntity> list(Integer page, Integer limit) {

        ProjectEntity projectEntity = new ProjectEntity();
        Integer newPage = (page - 1) * limit;
        projectEntity.setPageSize(limit);
        projectEntity.setStart(newPage);
        int count = Integer.parseInt(service.likeSelectCount(new ProjectEntity()).toString());

        List<ProjectEntity> select = service.select(projectEntity);
        return new LayUITable<>(0, "SUCCESS", count, select);
    }

    /**
     * 查询和当前登录员工相关的项目列表
     *
     * @return
     */
    @RequestMapping(value = "/list2", method = RequestMethod.GET)
    public LayUITable<ProjectEntity> list2(HttpSession session) {
        //获取session中的user从而得到staffID
        StaffEntity staff = (StaffEntity) session.getAttribute("user");
        Integer staffId = staff.getStaffId();
        StaffProjectEntity staffProjectEntity = new StaffProjectEntity();
        staffProjectEntity.setStaffId(staffId);
        //通过员工-项目表获取到项目
        List<StaffProjectEntity> staffProjectEntityList = staffProjectService.select(staffProjectEntity);
        Set<Integer> set = new HashSet<>();
        for (StaffProjectEntity entity : staffProjectEntityList) {
            //获取到这个员工的项目ID并去重
            set.add(entity.getProjectId());
        }
        List<ProjectEntity> list = new ArrayList<>();
        //遍历去重后的项目id进行查询项目信息
        for (Integer integer : set) {
            ProjectEntity projectEntity = new ProjectEntity();
            projectEntity.setProjectId(integer);
            List<ProjectEntity> select = service.select(projectEntity);
            if (select != null || select.size() != 0) {
                list.add(select.get(0));
            }
        }
        return new LayUITable<>(0, "SUCCESS", list.size(), list);
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody ProjectEntity entity) {
        service.update(entity);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
    }

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@RequestBody ProjectEntity entity) {
        service.add(entity);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody ProjectEntity entity) {
        service.delete(entity);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
    }

    /**
     * 项目分配员工
     *
     * @return
     */
    @RequestMapping(value = "/staff/set", method = RequestMethod.POST)
    public CommonResult staffSet(@RequestBody staffSet staffSet) {
        //判断该用户是否存在以及是不是技术人员
        StaffEntity staffEntity = new StaffEntity();
        staffEntity.setStaffId(staffSet.getStaffId());
        List<StaffEntity> staffEntities = staffService.select(staffEntity);
        if (staffEntities == null || staffEntities.size() == 0) {
            return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工不存在");
        }
        ProjectEntity projectEntity = new ProjectEntity();
        projectEntity.setProjectId(staffSet.getProjectId());
        //获取到这个项目
        List<ProjectEntity> select = service.select(projectEntity);
        ProjectEntity project = select.get(0);
        boolean exist;
        if (staffSet.getType() == 0) {
            //判断员工是不是技术人员
            if (staffEntities.get(0).getStaffType() != 0 && staffEntities.get(0).getStaffType() != 1) {
                return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工不是技术人员");
            }
            //员工
            switch (staffSet.getStaffType()) {
                //如果是模型
                case 1:
                    String modelStaff = project.getModelStaff();
                    exist = isExist(modelStaff, staffSet.getStaffId() + "");
                    if (exist) {
                        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工已在工程内");
                    }
                    if (modelStaff == null) {
                        modelStaff = staffSet.getStaffId() + "";
                    } else {
                        modelStaff += "," + staffSet.getStaffId();
                    }
                    project.setModelStaff(modelStaff);
                    break;
                //如果是渲染
                case 2:
                    String renderStaff = project.getRenderStaff();
                    exist = isExist(renderStaff, staffSet.getStaffId() + "");
                    if (exist) {
                        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工已在工程内");
                    }
                    if (renderStaff == null) {
                        renderStaff = staffSet.getStaffId() + "";
                    } else {
                        renderStaff += "," + staffSet.getStaffId();
                    }
                    project.setRenderStaff(renderStaff);
                    break;
                //如果是后期
                case 3:
                    String lastStaff = project.getLastStaff();
                    exist = isExist(lastStaff, staffSet.getStaffId() + "");
                    if (exist) {
                        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工已在工程内");
                    }
                    if (lastStaff == null) {
                        lastStaff = staffSet.getStaffId() + "";
                    } else {
                        lastStaff += "," + staffSet.getStaffId();
                    }
                    project.setLastStaff(lastStaff);
                    break;
                default:
            }
        } else {
            //主管
            //判断员工是不是主管
            if (staffEntities.get(0).getStaffType() != 3) {
                return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工不是主管");
            }
            switch (staffSet.getStaffType()) {
                case 1:
                    String modelHead = project.getModelHead();
                    exist = isExist(modelHead, staffSet.getStaffId() + "");
                    if (exist) {
                        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工已在工程内");
                    }
                    if (modelHead == null) {
                        modelHead = staffSet.getStaffId() + "";
                    } else {
                        modelHead += "," + staffSet.getStaffId();
                    }
                    project.setModelHead(modelHead);
                    break;
                case 2:
                    String renderHead = project.getRenderHead();
                    exist = isExist(renderHead, staffSet.getStaffId() + "");
                    if (exist) {
                        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工已在工程内");
                    }
                    //防止初始添加时防止报错
                    if (renderHead == null) {
                        renderHead = staffSet.getStaffId() + "";
                    } else {
                        renderHead += "," + staffSet.getStaffId();
                    }
                    project.setRenderHead(renderHead);
                    break;
                case 3:
                    String lastHead = project.getLastHead();
                    exist = isExist(lastHead, staffSet.getStaffId() + "");
                    if (exist) {
                        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG, "该员工已在工程内");
                    }
                    //防止初始添加时防止报错
                    if (lastHead == null) {
                        lastHead = staffSet.getStaffId() + "";
                    } else {
                        lastHead += "," + staffSet.getStaffId();
                    }
                    project.setLastHead(lastHead);
                    break;
                default:
            }
        }
        //插入到员工项目表中
        StaffProjectEntity staffProjectEntity = new StaffProjectEntity();
        staffProjectEntity.setProjectId(staffSet.getProjectId());
        staffProjectEntity.setStaffId(staffSet.getStaffId());
        staffProjectEntity.setTime(new Date(new java.util.Date().getTime()));
        staffProjectService.add(staffProjectEntity);

        //更新项目表
        service.update(project);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
    }

    /**
     * 判断该员工是否存在
     *
     * @param s
     */
    public Boolean isExist(String s, String staffId) {
        if (s == null) {
            return false;
        }
        String[] split = s.split(",");
        for (String s1 : split) {
            if (s1.equals(staffId)) {
                return true;
            }
        }
        return false;
    }
}
